<?php

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_filters.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_top_deals.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_mcs.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_references.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.airports.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.breadcrumb.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.lurls.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";

	$template = new template();	
	

	
if(!empty($_GET['destination_airport'])){
	

	$holidays = new holidays();
	$destinations = new destinations();
	$hotels = new hotels();
	$hotel_instances_mcs = new hotel_instances_mcs();
	$holiday_references = new holiday_references();
	$airports = new airports();
	$photos = new photos();
	$holiday_top_deals = new holiday_top_deals();
	$holiday_filters = new holiday_filters();
	$breadcrumb = new breadcrumb();
	$lurls = new lurls();	
	$tree = new tree();
	
	// Prepare search criteria
	$holiday_search_params = preprocess_search_form($_GET);

	// Create the request string
	$request = $holidays->create_cho_query($holiday_search_params);

	// Get the results
	$key = md5("micros_params_".implode(',',$holiday_search_params));
	
	$memcache = new Memcache;
	foreach(unserialize(CHOSTS) as $cache_server){
		 $memcache->addServer($cache_server, 11211);
	}
	
	//lookup value in memcache
	$result = $memcache->get($key);
			 
	if($result){
		$results = $result;

	}else{
		// Get the results
		$results = $holidays->connection_sabs($request);
		// Set cache
		$memcache->set($key,$results,0,600);
	}
	
	//unset($holiday_search_params);
	
	
	
	
	

	
		
	// Get destination using airport code
	$params['airport_code'] = $holiday_search_params['destination_airport'];
	$params['type_id'] = '1';
	$params['position'] = 'bottom';
	$params['cache']['seconds'] = '2400';
	$parent_destination_values = $destinations->destination_lookup($params);
	unset($params);
	
	// Get departure airport
	$params['airport_code'] = $holiday_search_params['departure_airport'];
	$params['cache']['seconds'] = '2400';
	$parent_departure_airport_values = $airports->get_airport($params);
	unset($params);
	
	
	// Get the month of the search 
	$tidy_number = str_replace("0", "", substr($holiday_search_params['departure_date'],2,2));
	$search_month = date('F',mktime(0,0,0,$tidy_number,1,2006));
	
	
	// Template
	$template->title = $parent_destination_values[0]['name']." holidays from ".$parent_departure_airport_values[0]['full']." in ".$search_month;
	$template->meta_keywords = $parent_destination_values['name']." holidays, last minute, package holidays, from ".$parent_departure_airport_values[0]['full'].", Ireland, ".$search_month;
	$template->meta_description = "Book last minute holidays to ".$parent_destination_values[0]['name']." from ".$parent_departure_airport_values[0]['full']." in ".$search_month;
	$template->section_highlight = "holidays";
	$template->search_tab = "holidays";
	$template->left_col = true;
	$template->breadcrumbs_html = "<li>&rsaquo;</li><li title=\"Holidays\"><a href=\"/holidays/index.html\" title=\"Holidays\">Holidays</a></li><li>&rsaquo;</li><li title=\"Holiday Search Results\"><a href=\"/holidays/results_list.html\" title=\"Holidays Search Results\">Search Results</a></li>";
	$template->ld_head();


	// ASYNC!	
	// Delete old realted deals 
	$holiday_top_deals->delete_related_top_deals_holidays($holiday_search_params);
				

	//print_r($results);
	$l = 0; // number of coshure holidays
	
	foreach($results as $value) {

	// See if the row contains holiday details
	if($value[1] == 'HOL'){


	
		unset($holiday_values,$holiday_string,$hotel_values,$accommodation_name,$accommodation_photos_url,$lead_accommodation_thumb_url,$url_string,$reference_id,$mod_price,$base_price);
		
		$holiday_string = trim(implode('~',$value));
		
		$holiday_values = $holiday_references->build_holiday_keys($value);
		

		// Find existing reference
		$cache['seconds'] = '600';
		$existing_reference_values = $holiday_references->find_identical_reference($holiday_string, $cache);
		unset($cache);
		
		if($existing_reference_values){
			// Get existing reference id
			$reference_id = $existing_reference_values[0]['reference_id'];	
		}else{
			// Insert a reference id and output it
			$reference_id = $holiday_references->insert_reference($holiday_string);	
		}


		// Filters
		$filter_out = false;
		// Only use active tour opertors
		if(in_array($holiday_values['tour_op'], $holidays->inactive_tos)){
			$filter_out = true;
		}
		// Holiday filters
		if($holiday_filters->find_filtered_holiday($reference_id)){
			$filter_out = true;
		}
		unset($params);
		// flight filters
		if($holiday_filters->find_filtered_flight($holiday_values)){
			$filter_out = true;
		}
		if(!$filter_out){
		
		
		// See if the price has been filtered
		$mod_price = $holiday_filters->find_holiday_mod_price($reference_id, true);
		if($mod_price){
			echo "hello";
			$effective_price_adult = $mod_price['mod_price'];
			if($mod_price['price_adult']) $effective_price_adult = $mod_price['price_adult'];
		}else{
			
			$effective_price_adult = $holiday_values['price_adult']; 
		}
		
		
		

		// Get the display price
		$params['price'] = $effective_price_adult;
		$params['departure_airport'] = $value[8];
		$params['tour_op'] = $value[6];
		$display_price = $holidays->display_price($params);
		unset($params);

	
	/////////////////////////////////////////////////////////////////
	// ACCOMMODATION, RESORT AND DESTINATION LOOKUPS - results_list.php - 15/05/2009
	/////////////////////////////////////////////////////////////////
	
	$holiday_values['hotel_name'] = ucwords(strtolower($holiday_values['hotel_name']));
	$holiday_values['resort'] = ucwords(strtolower($holiday_values['resort']));



	// Accommidation lookup
	
	unset($hotel_values, $hotel_found, $hotel_photos, $resort_found, $resort_values, $desination_values);
	
	$params = array();
    $params['supplier_code'] = 'mcs';
    $params['tour_op'] = $holiday_values['tour_op'];
	$params['hotel_name'] = $holiday_values['hotel_name'];
	$params['destination_airport'] = $holiday_values['destination_airport'];
	$params['resort'] = $holiday_values['resort'];
	$params['travel_code'] = $holiday_values['travel_code'];
	$params['resort_code'] = $holiday_values['resort_code'];
	$params['holiday_code'] = $holiday_values['holiday_code'];
	$params['hotel_code'] = $holiday_values['hotel_code'];
	$params['cache']['seconds'] = '2400';
	$lookup = $hotels->lookup_hotel($params);
	unset($params);
	
	// Set values
	$hotel_values = $lookup['hotel_values'];
	$resort_values = $lookup['resort_values'];
	$destination_values = $lookup['destination_values'];
	
	// Get photo url
	$lead_hotel_thumb_url = $tree->photo_url($hotel_values['main_photo']);
	
	
	if(!empty($hotel_values['tree_id'])){
		$hotel_tree_id = $hotel_values['tree_id'];
	}else{
		$hotel_tree_id = NULL;
	}
	
	// Insert cheapest / first holiday into top deals
	if($l == 0){
		
		$top_deal_params = array(
		'reference_id' => $reference_id,					 
		'tour_op' => $value[6],
		'departure_date' => $value[7],
		'return_date' => $value[12],
		'nights' => $value[14],
		'destination_airport' => $value[9],
		'departure_airport' => $value[8],
		'board_basis' => $value[30],
		'price' => $display_price,
		'hotel_tree_id' => NULL
		);	
		
		// ASYNC!
		if($top_deal_params['nights'] > 0)	$holiday_top_deals->insert_top_deal($top_deal_params);
		
	}
		
	// Departure airport lookup
	$params['airport_code'] = $holiday_values['departure_airport'];
	$params['cache']['seconds'] = '600';
	$departure_airport_values = $airports->get_airport($params);
	unset($params);
	
	// Destination airport lookup
	$params['airport_code'] = $holiday_values['destination_airport'];
	$params['cache']['seconds'] = '600';
	$destination_airport_values = $airports->get_airport($params);
	unset($params);


	// Get LURL
	$params['reference_id'] = $reference_id;
	//$params['tree_id'] = $hotel_values['tree_id'];
	$url_string = $lurls->get_holiday_lurl($params);
	unset($params);


	// URL title string
	$item_title = "Last minute holiday to ".$resort_values['name']." ".$destination_values['name'];
	
	$results_array = array();
	$results_array['url_string'] = $url_string;
	$results_array['lead_hotel_thumb_url'] = $lead_hotel_thumb_url;
	$results_array['hotel_values']['name'] = $hotel_values['name'];
	$results_array['item_title'] = $item_title;
	$results_array['destination_values']['name'] = $destination_values['name'];
	$results_array['resort_values']['name'] = $resort_values['name'];
	$results_array['rating'] = $value[20];
	$results_array['nights'] = $value[14];
	$results_array['board_basis'] = $value[30];
	$results_array['departure_date'] = display_sabs_date($value[7],'l j F Y');
	$results_array['departure_airport'] = $departure_airport_values[0]['simple']." (".$value[8].") ";
	$results_array['return_date'] = display_sabs_date($value[12],'l j F Y');
	$results_array['url_string'] = $url_string;
	$results_array['destination_airport'] = $destination_airport_values[0]['simple']." (".$value[9].") ";
	$results_array['display_price'] = $display_price;
	
	$results_array_final[] = $results_array;
	
	// number of filtered holidays
	$l++; 
	
		}
	
	}

}

	
if($results_array_final){	
	include SITE_ROOT_PATH."/_lib/html/holiday_results_list.php";
}else{
	// No holiday results	
	include SITE_ROOT_PATH."/_lib/html/holiday_no_results.php";
}

	$template->ld_foot();

}else{

	header("Location: /holidays/");
    exit;

}


	
	







function preprocess_search_form($params) {

	$holiday_search = array();
	
	$holiday_search['departure_airport'] = $params['departure_airport'];
	$holiday_search['destination_airport'] = $params['destination_airport'];
	$holiday_search['departure_date'] = $params['departure_date'];
	$holiday_search['nights'] = $params['nights'];
	
	if(empty($params['pax_adult'])){
		$holiday_search['pax_adult'] = 2;
	}else{
		$holiday_search['pax_adult'] = $params['pax_adult'];
	}
	
	if(empty($params['pax_child'])){
		$holiday_search['pax_child'] = 0;
	}else{
		$holiday_search['pax_child'] = $params['pax_child'];
	}

    return $holiday_search;
}

	
?>